/******************************************
@Description:更改商家安全码颜色
@Time : 2021/2/1 15:18
@Author :lixiaojun

*******************************************/
package main

import (
	"context"
	"git.900sui.cn/kc/base/utils"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/bus"
	busEs "git.900sui.cn/kc/rpcinterface/client/elastic/bus"
	bus2 "git.900sui.cn/kc/rpcinterface/interface/bus"
	"github.com/streadway/amqp"
	"strconv"
	"strings"
)

func main() {
	rpcEs := new(busEs.Shop).Init()
	rpcBus := new(bus.Bus).Init()
	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	new(utils.KcLog).GetInstance(kcgin.AppConfig.String("bus.log.riskBusSafeCode"))

	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("bus.queue.riskBusSafeCode"), func(msg amqp.Delivery) {
		dataStr := string(msg.Body)
		dataSlice := strings.Split(dataStr, "_")
		if len(dataSlice) >= 7 {
			//riskBusId_风险状况值_安全码颜色值_省id_市id_区id_街道id
			//35_1_2_8_112_976_0
			sData := &bus2.ArgsUpdateRiskBusSafeCode{}
			sData.RiskBusId, _ = strconv.Atoi(dataSlice[0])
			sData.Rank, _ = strconv.Atoi(dataSlice[1])
			sData.SafeCodeColor, _ = strconv.Atoi(dataSlice[2])
			sData.Pid, _ = strconv.Atoi(dataSlice[3])
			sData.Cid, _ = strconv.Atoi(dataSlice[4])
			sData.Did, _ = strconv.Atoi(dataSlice[5])
			sData.Tid, _ = strconv.Atoi(dataSlice[6])
			var rpcBusRes bus2.ReplyUpdateRiskBusSafeCode
			if err := rpcBus.UpdateRiskBusSafeCode(context.Background(), sData, &rpcBusRes); err != nil {
				utils.Logerr.Printf("更改商家安全码失败，riskBusId：【%s】失败信息：%s\n", dataSlice[0], err.Error())
			}
			busId := rpcBusRes.BusId
			if busId > 0 {
				var rpcEsRes bool
				if err := rpcEs.SetShopSafeCode(context.Background(), &busId, &rpcEsRes); err != nil {
					utils.Logerr.Printf("更新门店预付码失败，失败信息：%s\n", err.Error())
				}
			}
		}
	})

	if err != nil {
		utils.Logerr.Printf("更改商家安全码错误，错误信息：%s\n", err.Error())
	}
}
